package com.lyf.emall.controller;

import com.lyf.emall.bean.Page;
import com.lyf.emall.bean.ResponseResult;
import com.lyf.emall.bean.SysAuthority;
import com.lyf.emall.bean.SysUser;
import com.lyf.emall.service.AuthorityService;
import com.lyf.emall.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;

/**
 * @Description:
 * @Author Lfy
 * @Date 2022/4/8-15:22
 */
@RestController
public class AuthorityController {

    @Autowired
    private AuthorityService authorityService;

    @GetMapping("/authority")
    @PreAuthorize("hasAuthority('/authority/list')")
    public ResponseResult getAllAuthority(Page page){
        return authorityService.getAllAuthority(page);
    }

    @GetMapping("/authority/role")
    @PreAuthorize("hasAuthority('/authority/role')")
    public ResponseResult getAllAuthority(){
        return authorityService.getAllAuthority();
    }

    @GetMapping("/authority/repeat")
    @PreAuthorize("hasAuthority('/authority/repeat')")
    public ResponseResult isAuthority(@RequestParam("authorityKey") String authorityKey){
        return authorityService.isAuthority(authorityKey);
    }

    @PostMapping("/authority")
    @PreAuthorize("hasAuthority('/authority/add')")
    public ResponseResult addAuthority(@RequestParam("authorityName") String authorityName,
                                       @RequestParam("authorityKey") String authorityKey){
        if (!StringUtils.hasText(authorityName) || !StringUtils.hasText(authorityKey)){
            return new ResponseResult(400, "字段不能为空");
        }
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        SysAuthority sysAuthority = new SysAuthority(null, authorityName, authorityKey, 1, 1, timestamp, timestamp, 1, null);

        return authorityService.addAuthority(sysAuthority);
    }

    @PutMapping("/authority")
    @PreAuthorize("hasAuthority('/authority/put')")
    public ResponseResult updateAuthority(@RequestParam("authorityId") String authorityId,
                                          @RequestParam("authorityName") String authorityName,
                                          @RequestParam("authorityKey") String authorityKey,
                                          @RequestHeader String token) throws Exception {
        // 解析token
        Claims claims = JwtUtil.parseJWT(token);
        String userId = claims.getSubject();

        SysAuthority sysAuthority = new SysAuthority(Integer.valueOf(authorityId), authorityName, authorityKey, null, null, null, null, Integer.valueOf(userId), null);
        return authorityService.updateAuthority(sysAuthority);
    }

    @DeleteMapping("/authority")
    @PreAuthorize("hasAuthority('/authority/delete')")
    public ResponseResult deleteAuthority(@RequestParam("authorityId") String authorityId){
        return authorityService.deleteAuthority(Integer.valueOf(authorityId));
    }
}
